package com.fan.controller;

import com.fan.entity.Article;
import com.fan.entity.User;
import com.fan.service.CreateService;
import com.fan.service.UserService;
import com.fan.service.Impl.LoginServiceImpl;
import com.fan.service.Impl.UserServiceImpl;
import com.fan.utils.AjaxResult;
import com.fan.utils.ResponseResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.HashMap;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
public class UserController {

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private CreateService createService;

    @Autowired
    private LoginServiceImpl loginService;

//
//    @GetMapping("/login")
//    public AjaxResult login(@Param("userName") String userName, @Param("passWord") String passWord) {
//        System.out.println(userName + " === " + passWord);
//        User user = userService.login(userName, passWord);
//        if (user != null) {
//            // 将传给前端的信息存入map
//            HashMap<String,Object> map = new HashMap<String,Object>();
//            map.put("userId",user.getUserId());
//            map.put("userName",user.getUserName());
//            return AjaxResult.success("操作成功",map);
//        } else {
//            return AjaxResult.error("操作失败");
//        }
//    }

    @GetMapping("/login")
    public ResponseResult testLogin(@Param("userName") String userName, @Param("passWord") String passWord) {
        User user = new User(userName,passWord);
//        user.setPassWord(new BCryptPasswordEncoder().encode(passWord));
        return loginService.login(user);
    }


    @GetMapping("/findByName")
    public AjaxResult selectByName(String userName){
        System.out.println(userName);
        User user = userService.selectByName(userName) ;
        if(user != null){
            //用户名已经存在
            return  AjaxResult.error("操作失败");
        }else{
            //用户名不存在，可以注册
            return AjaxResult.success("操作成功");
        }
    }
    @GetMapping("/register")
    public AjaxResult insertUser(int userId,String userName,String passWord){
        System.out.println(userId+' '+userName +' '+passWord);
        int user = userService.insertUser(userId,userName,passWord);
        if(user == 1){
            //注册成功
            return AjaxResult.success("操作成功");
        }else{
            //注册失败
            return  AjaxResult.error("操作失败");
        }
    }
    @GetMapping("/getCount")
    public int getCount(){
        int count = userService.getCount();
        System.out.println(count);
        return count;
    }

    @GetMapping("getHeatWriter")
    public AjaxResult getHeatWriter(){
        //获取全部用户信息
        List<User> list = userService.getAllUser();
        //获取每个用户的粉丝量
        for (User user : list){
           int fan =  createService.getFansByUser(user.getUserId()).toArray().length;
           user.setFanCount(fan);
        }
        //按粉丝量排序
        list = list.stream().sorted(Comparator.comparing(User::getFanCount)
                        .reversed())
                .collect(Collectors.toList());
        return AjaxResult.success("作者榜单请求成功！",list.subList(0,3));
    }

    @GetMapping("/userLayout")
    public ResponseResult logout(@Param("userId") int userId) {
        System.out.println("userId=="+userId);
        return loginService.logout(userId);
    }
}
